package com.d09cnpm2.group8.map;

import java.util.ArrayList;
import java.util.Map;

import com.d09cnpm2.group8.MainActivity;
import com.d09cnpm2.group8.model.TravelLocation;
import com.google.android.gms.maps.model.LatLng;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class GetDistancesAsyntask extends
	AsyncTask<ArrayList<TravelLocation>, Object, ArrayList<String>> {
    private LatLng currentLocation;
    private MainActivity activity;
    private ProgressDialog progressDialog;
    private String TAG = "---------distance asynctask----------->";

    public GetDistancesAsyntask(MainActivity activity, LatLng currentLocation) {
	super();
	this.currentLocation = currentLocation;
	this.activity = activity;
    }

    @Override
    protected ArrayList<String> doInBackground(
	    ArrayList<TravelLocation>... params) {
	ArrayList<String> distances = new ArrayList<String>();
	ArrayList<TravelLocation> locations = params[0];
	Log.e(TAG, "do in background" + locations.size());
	 for (int i = 0; i < locations.size(); i++) {
	 GMapV2Direction direction = new GMapV2Direction();
	 LatLng dest = new LatLng(locations.get(i).lat, locations.get(i).lng);
	 String result = direction.getJsonResult(currentLocation, dest);
	 Log.e(TAG , result);
	 if (result != null) {
	 String distanceString = direction.getDistance(result);
	 distances.add(distanceString);
	 }
	 }
	return distances;
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
	progressDialog.dismiss();
	activity.fetchDistances(result);
    }

    @Override
    protected void onPreExecute() {
	progressDialog = new ProgressDialog(activity);
	progressDialog.show();
    }
}
